#include "sise_packer.h"
#include "dataexchange.h"

#ifdef __cplusplus
extern "C" {
#endif

/*
typedef struct attacker_t {
    int id;
    int type;
    float x;
    float y;
} attacker_t;
**/

int packed_size_attacker_t()
{
	attacker_t *pStruct, temp;
	int packedSize = 0;
	pStruct = &temp;
	
	packedSize += sizeof(pStruct->id);
	packedSize += sizeof(pStruct->type);
	packedSize += sizeof(pStruct->x);
	packedSize += sizeof(pStruct->y);
	
	return packedSize;
}

int sise_pack_attacker_t(char* pBin, attacker_t* pStruct)
{
	int packedSize = 0;
	
	sise_var2bin_int(&pBin[packedSize], &pStruct->id);
	packedSize += sizeof(pStruct->id);
	
	sise_var2bin_int(&pBin[packedSize], &pStruct->type);
	packedSize += sizeof(pStruct->type);
	
	sise_var2bin_float(&pBin[packedSize], &pStruct->x);
	packedSize += sizeof(pStruct->x);
	
	sise_var2bin_float(&pBin[packedSize], &pStruct->y);
	packedSize += sizeof(pStruct->y);
	
	return packedSize;
}

int sise_unpack_attacker_t(char* pBin, attacker_t* pStruct)
{
	int packedSize = 0;
	
	sise_bin2int(&pBin[packedSize], &pStruct->id);
	packedSize += sizeof(pStruct->id);
	
	sise_bin2int(&pBin[packedSize], &pStruct->type);
	packedSize += sizeof(pStruct->type);
	
	sise_bin2float(&pBin[packedSize], &pStruct->x);
	packedSize += sizeof(pStruct->x);
	
	sise_bin2float(&pBin[packedSize], &pStruct->y);
	packedSize += sizeof(pStruct->y);
	
	return packedSize;
}

/*
typedef struct __TxPackage {
	float	angle;	//when angle==0 means no need to fire, otherwise (0,180] means fire
	int     weaponType;
	long	hitedAttackerID;//the attacker will be hit in next period
	long    th;
	int	hit;
} Tx;
**/

int packed_size_Tx()
{
	Tx *pStruct, temp;
	int packedSize = 0;
	pStruct = &temp;
	
	packedSize += sizeof(pStruct->angle);
	packedSize += sizeof(pStruct->weaponType);
	packedSize += sizeof(pStruct->hitedAttackerID);
	packedSize += sizeof(pStruct->th);
	packedSize += sizeof(pStruct->hit);
	
	return packedSize;
}

int sise_pack_Tx(char* pBin, Tx* pStruct)
{
	int packedSize = 0;
	
	sise_var2bin_float(&pBin[packedSize], &pStruct->angle);
	packedSize += sizeof(pStruct->angle);
	
	sise_var2bin_int(&pBin[packedSize], &pStruct->weaponType);
	packedSize += sizeof(pStruct->weaponType);
	
	sise_var2bin_long(&pBin[packedSize], &pStruct->hitedAttackerID);
	packedSize += sizeof(pStruct->hitedAttackerID);
	
	sise_var2bin_long(&pBin[packedSize], &pStruct->th);
	packedSize += sizeof(pStruct->th);
	
	sise_var2bin_int(&pBin[packedSize], &pStruct->hit);
	packedSize += sizeof(pStruct->hit);
	
	return packedSize;
}

int sise_unpack_Tx(char* pBin, Tx* pStruct)
{
	int packedSize = 0;
	
	sise_bin2float(&pBin[packedSize], &pStruct->angle);
	packedSize += sizeof(pStruct->angle);
	
	sise_bin2int(&pBin[packedSize], &pStruct->weaponType);
	packedSize += sizeof(pStruct->weaponType);
	
	sise_bin2long(&pBin[packedSize], &pStruct->hitedAttackerID);
	packedSize += sizeof(pStruct->hitedAttackerID);
	
	sise_bin2long(&pBin[packedSize], &pStruct->th);
	packedSize += sizeof(pStruct->th);
	
	sise_bin2int(&pBin[packedSize], &pStruct->hit);
	packedSize += sizeof(pStruct->hit);
	
	return packedSize;
}

#ifdef __cplusplus
}
#endif
